package myzzyl.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import myzzyl.constants.ErrorConstants;
import myzzyl.domain.dto.RoomTypeDto;
import myzzyl.domain.pojo.RoomType;
import myzzyl.domain.vo.RoomTypeVo;
import myzzyl.exception.BusinessException;
import myzzyl.mapper.RoomTypeMapper;
import myzzyl.service.RoomService;
import myzzyl.service.RoomTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 33249
* @description 针对表【room_type】的数据库操作Service实现
* @createDate 2025-08-07 20:07:43
*/
@Service
public class RoomTypeServiceImpl extends ServiceImpl<RoomTypeMapper, RoomType>
    implements RoomTypeService {

    @Autowired
    private RoomService roomService;

    /**
     * 获取所有房型
     * @return 返回值
     */
    @Override
    public List<RoomTypeVo> getAll() {
        // 获取所有房型
        List<RoomType> roomTypeList = list();

        // 封装Vo对象返回
        List<RoomTypeVo> roomTypeVoList = roomTypeList.stream().map(roomType -> {
            RoomTypeVo roomTypeVo = new RoomTypeVo();
            BeanUtils.copyProperties(roomType, roomTypeVo);
            roomTypeVo.setCreator("默认用户名");

            // 根据房型查询房间数量
            Long roomCount = roomService.selectCountByRoomTypeId(roomType.getName());
            roomTypeVo.setRoomCount(roomCount.intValue());
            return roomTypeVo;
        }).toList();

        return roomTypeVoList;
    }

    /**
     * 获取房型详情
     * @param id 房型id
     * @return 返回值
     */
    @Override
    public RoomTypeVo getInfo(Long id) {
        // 根据id查询房型
        RoomType roomType = getById(id);
        if(roomType == null) {
            throw new BusinessException(ErrorConstants.ROOMTYPE_IS_NOT_EXIST);
        }

        RoomTypeVo roomTypeVo = new RoomTypeVo();
        BeanUtils.copyProperties(roomType, roomTypeVo);
        roomTypeVo.setCreator("默认用户名");
        return roomTypeVo;
    }


    /**
     * 新增房型
     * @param roomTypeDto 房型表单包装类
     * @return 返回值
     */
    @Override
    public Boolean add(RoomTypeDto roomTypeDto) {
        // 查询房型是否已经存在
        if(lambdaQuery().eq(StrUtil.isNotBlank(roomTypeDto.getName()), RoomType::getName,
                roomTypeDto.getName()).exists()) {
            throw new BusinessException(ErrorConstants.ROOMTYPE_IS_ALREADY_EXIST);
        }

        // 添加房型
        RoomType roomType = new RoomType();
        BeanUtils.copyProperties(roomTypeDto, roomType);
        roomType.setCreateTime(new Date());
        roomType.setUpdateTime(new Date());
        roomType.setCreateBy(1L);
        roomType.setUpdateBy(1L);

        return save(roomType);
    }

    /**
     * 编辑房型
     * @param roomTypeDto 房型表单包装类
     * @return 返回值
     */
    @Override
    public Boolean edit(RoomTypeDto roomTypeDto) {
        // 查询房型是否已经存在（除去自己）
        if(lambdaQuery()
                .eq(StrUtil.isNotBlank(roomTypeDto.getName()), RoomType::getName, roomTypeDto.getName())
                .ne(roomTypeDto.getId() != null, RoomType::getId, roomTypeDto.getId())
                .exists()) {
            throw new BusinessException(ErrorConstants.ROOMTYPE_IS_ALREADY_EXIST);
        }

        // 添加房型
        return lambdaUpdate()
                .set(StrUtil.isNotBlank(roomTypeDto.getName()), RoomType::getName, roomTypeDto.getName())
                .set(StrUtil.isNotBlank(roomTypeDto.getPhoto()), RoomType::getPhoto, roomTypeDto.getPhoto())
                .set(StrUtil.isNotBlank(roomTypeDto.getIntroduction()), RoomType::getIntroduction, roomTypeDto.getIntroduction())
                .set(roomTypeDto.getStatus() != null, RoomType::getStatus, roomTypeDto.getStatus())
                .set(roomTypeDto.getPrice() != null, RoomType::getPrice, roomTypeDto.getPrice())
                .eq(RoomType::getId, roomTypeDto.getId())
                .update();
    }


    /**
     * 删除房型
     * @param id 房型id
     * @return 返回值
     */
    @Override
    public Boolean deleteRoomTypeById(Long id) {
        // 判断要删除的房型是否存在
        if(!lambdaQuery().eq(id != null, RoomType::getId, id).exists()) {
            throw new BusinessException(ErrorConstants.ROOMTYPE_IS_NOT_EXIST);
        }
        //todo 判断压迫删除的房型是否关联的房间

        // 删除房型
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 启用or禁用房型
     * @param id 房型id
     * @param status 状态
     * @return 返回值
     */
    @Override
    public Boolean enableOrDisable(Long id, Integer status) {
        // 判断房型是否存在
        if(!lambdaQuery().eq(id != null, RoomType::getId, id).exists()) {
            throw new BusinessException(ErrorConstants.ROOMTYPE_IS_NOT_EXIST);
        }

        // 更新房型状态
        return lambdaUpdate()
                .set(status != null, RoomType::getStatus, status)
                .eq(id != null, RoomType::getId, id)
                .update();
    }
}




